﻿using HomeCloudStorageSystem.DataBase;
using HomeCloudStorageSystem.Helps;

namespace HomeCloudStorageSystem.Services
{
    public class AvatarsRepository : IAvatarsRepository
    {
        private readonly AppDbContext _context;
        public AvatarsRepository(AppDbContext context)
        {
            _context = context;
        }
        public string SavaAvatars(IFormFile file, string filePath)
        {
            try
            {
                // 保存图片
                byte[] fileByte = ReadStreamHelp.ReadStream(file.OpenReadStream());
                // 用户头像命名规则：Guid + 图片扩展名，已经存在头像的用户，修改头像时文件名不变，只修改扩展名
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    fileStream.Write(fileByte, 0, fileByte.Length);
                }
                return "上传成功";
            }
            catch
            {
                File.Delete(filePath);
                return "上传失败";
            }
        }

        public byte[] Avatars(string fileFullPath)
        {
            string filePath = @"../../家庭云盘系统文件夹/用户头像/" + fileFullPath;
            //读取文件为字节流
            byte[] fileBytes = File.ReadAllBytes(filePath);
            return fileBytes;
        }
    }
}
